/*
 * Copyright (c) 2012, marco.tamburelli@gmail.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: 
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer. 
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution. 
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package org.bmi.gwt.ws.rebind.util;

import com.google.gwt.core.ext.BadPropertyValueException;
import com.google.gwt.core.ext.ConfigurationProperty;
import com.google.gwt.core.ext.PropertyOracle;
import com.google.gwt.core.ext.TreeLogger;
import com.google.gwt.core.ext.UnableToCompleteException;
import com.google.gwt.core.ext.typeinfo.JClassType;
import com.google.gwt.core.ext.typeinfo.JRealClassType;
import com.google.gwt.util.regexfilter.RegexFilter;

import java.util.List;

/**
 * Copied from GWT internal code
 */
public class BlacklistTypeFilter implements TypeFilter
{
	private static String PROP_WS_BLACKLIST = "ws.blacklist";

	/**
	 * Configure {@link RegexFilter} for use for blacklists.
	 */
	private static class WsBlacklist extends RegexFilter
	{
		public WsBlacklist(TreeLogger logger, List<String> regexes) throws UnableToCompleteException
		{
			super(logger, regexes);
		}

		@Override
		protected boolean acceptByDefault()
		{
			return true;
		}

		@Override
		protected boolean entriesArePositiveByDefault()
		{
			return false;
		}
	}

	private final WsBlacklist wsBlacklist;
	private TreeLogger logger;

	public BlacklistTypeFilter(TreeLogger logger, PropertyOracle propertyOracle) throws UnableToCompleteException
	{
		ConfigurationProperty prop;
		try
		{
			prop = propertyOracle.getConfigurationProperty(PROP_WS_BLACKLIST);
		}
		catch (BadPropertyValueException e)
		{
			logger.log(TreeLogger.ERROR, "Could not find property " + PROP_WS_BLACKLIST);
			throw new UnableToCompleteException();
		}

		this.logger = logger.branch(TreeLogger.DEBUG, "Analyzing WS blacklist information");
		wsBlacklist = new WsBlacklist(logger, prop.getValues());
	}

	@Override
	public String getName()
	{
		return "BlacklistTypeFilter";
	}

	@Override
	public boolean isAllowed(JClassType type)
	{
		String name = getBaseTypeName(type);

		/* For types not handled by getBaseTypeName just return true. */
		if (name == null) { return true; }

		return wsBlacklist.isIncluded(logger, name);
	}

	/**
	 * Returns a simple qualified name for simple types, including classes and
	 * interfaces, parameterized, and raw types. Null is returned for other
	 * types such as arrays and type parameters (e.g., 'E' in java.util.List<E>)
	 * because filtering is meaningless for such types.
	 */
	private String getBaseTypeName(JClassType type)
	{
		JClassType baseType = null;

		if (type instanceof JRealClassType)
		{
			baseType = type;
		}
		else if (type.isParameterized() != null)
		{
			baseType = type.isParameterized().getBaseType();
		}
		else if (type.isRawType() != null)
		{
			baseType = type.isRawType();
		}

		return baseType == null ? null : baseType.getQualifiedSourceName();
	}
}
